home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / csfilesc / filetool.ctl (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1998-10-01  |  13.5 KB  |  380 lines

  1. VERSION 5.00
  2. Begin VB.UserControl FileTool 
  3.    CanGetFocus     =   0   'False
  4.    ClientHeight    =   330
  5.    ClientLeft      =   0
  6.    ClientTop       =   0
  7.    ClientWidth     =   360
  8.    InvisibleAtRuntime=   -1  'True
  9.    Picture         =   "FileTool.ctx":0000
  10.    ScaleHeight     =   330
  11.    ScaleWidth      =   360
  12.    ToolboxBitmap   =   "FileTool.ctx":018A
  13. Attribute VB_Name = "FileTool"
  14. Attribute VB_GlobalNameSpace = False
  15. Attribute VB_Creatable = True
  16. Attribute VB_PredeclaredId = False
  17. Attribute VB_Exposed = True
  18. Option Explicit
  19. Private Declare Function BinSearchPath Lib "kernel32" Alias "SearchPathA" (ByVal lpPath As String, ByVal lpFileName As String, ByVal lpExtension As String, ByVal nBufferLength As Long, ByVal lpBuffer As String, ByVal lpFilePart As String) As Long
  20. Private Declare Function GetBinaryType Lib "kernel32" Alias "GetBinaryTypeA" (ByVal lpApplicationName As String, lpBinaryType As Long) As Long
  21. Private Declare Function apiGetVersion Lib "kernel32" Alias "GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) As Long
  22. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
  23. Private Type SHITEMID 'mkid
  24.     cb As Long
  25.     abID As Byte
  26. End Type
  27. Private Type ITEMIDLIST 'idl
  28.     mkid As SHITEMID
  29. End Type
  30. Private Type BROWSEINFO 'bi
  31.     hOwner As Long
  32.     pidlRoot As Long
  33.     pszDisplayName As String
  34.     lpszTitle As String
  35.     ulFlags As Long
  36.     lpfn As Long
  37.     lParam As Long
  38.     iImage As Long
  39. End Type
  40.         
  41. Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" _
  42.     (ByVal pidl As Long, ByVal pszPath As String) As Long
  43. Private Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" _
  44.     (lpBrowseInfo As BROWSEINFO) As Long
  45. Private Type OPENFILENAME
  46.     lStructSize As Long
  47.     hwndOwner As Long
  48.     hInstance As Long
  49.     lpstrFilter As String
  50.     lpstrCustomFilter As String
  51.     nMaxCustFilter As Long
  52.     nFilterIndex As Long
  53.     lpstrFile As String
  54.     nMaxFile As Long
  55.     lpstrFileTitle As String
  56.     nMaxFileTitle As Long
  57.     lpstrInitialDir As String
  58.     lpstrTitle As String
  59.     Flags As Long
  60.     nFileOffset As Integer
  61.     nFileExtension As Integer
  62.     lpstrDefExt As String
  63.     lCustData As Long
  64.     lpfnHook As Long
  65.     lpTemplateName As String
  66. End Type
  67. Private Type OSVERSIONINFO
  68.         dwOSVersionInfoSize As Long
  69.         dwMajorVersion As Long
  70.         dwMinorVersion As Long
  71.         dwBuildNumber As Long
  72.         dwPlatformId As Long
  73.         szCSDVersion As String * 128
  74. End Type
  75. Private Const BIF_RETURNONLYFSDIRS = &H1
  76. Private Const SCS_32BIT_BINARY = 0
  77. Private Const SCS_DOS_BINARY = 1
  78. Private Const SCS_WOW_BINARY = 2
  79. Private Const SCS_PIF_BINARY = 3
  80. Private Const SCS_POSIX_BINARY = 4
  81. Private Const SCS_OS216_BINARY = 5
  82. Public Enum CSBinaryTypes
  83.     NonExecutable
  84.     BinaryWin_32Bit
  85.     BinaryDos
  86.     BinaryOS2_16Bit
  87.     BinaryPIF
  88.     BinaryPosix
  89.     BinaryWin_16Bit
  90. End Enum
  91. 'Default Property Values:
  92. Const m_def_SearchPath = ""
  93. Const m_def_File = ""
  94. Const m_def_FilterIndex = 1
  95. Const m_def_Filters = "All Files (*.*)|*.*"
  96. 'Property Variables:
  97. Dim m_SearchPath As String
  98. Dim m_File As String
  99. Dim m_Filters As String
  100. Dim m_FilterIndex As Long
  101. Public Property Get FilterIndex() As Long
  102. Attribute FilterIndex.VB_Description = "Returns or sets a default filter for SelectFile or SelectSave dialog box."
  103. Attribute FilterIndex.VB_HelpID = 2019
  104.     On Error Resume Next
  105.     FilterIndex = m_FilterIndex
  106. End Property
  107. Public Property Let FilterIndex(ByVal newIndex As Long)
  108.     On Error Resume Next
  109.     m_FilterIndex = Abs(newIndex)
  110.     PropertyChanged "FilterIndex"
  111. End Property
  112. Public Property Get Filters() As String
  113. Attribute Filters.VB_Description = "Returns or sets the filters that are displayed in the Type list box of a SelectFile or SelectSave dialog box."
  114. Attribute Filters.VB_HelpID = 2018
  115.     On Error Resume Next
  116.     Filters = m_Filters
  117. End Property
  118. Public Property Let Filters(ByVal newFilters As String)
  119.     On Error Resume Next
  120.     m_Filters = newFilters
  121.     PropertyChanged "Filters"
  122. End Property
  123. Private Property Get FileFilters() As String
  124.     Dim sFilters As String
  125.     sFilters = m_Filters & Chr$(0)
  126.     While (InStr(1, sFilters, "|") > 0)
  127.         Mid(sFilters, InStr(1, sFilters, "|"), 1) = Chr(0)
  128.     Wend
  129.     FileFilters = sFilters
  130. End Property
  131. Public Property Get SearchPath() As String
  132. Attribute SearchPath.VB_Description = "Sets the path or paths to search."
  133. Attribute SearchPath.VB_HelpID = 2011
  134.     On Error Resume Next
  135.     If Trim(m_SearchPath) = "" Then
  136.         SearchPath = Environ("PATH")
  137.     Else
  138.         SearchPath = m_SearchPath
  139.     End If
  140. End Property
  141. Public Property Let SearchPath(ByVal New_SearchPath As String)
  142.     On Error Resume Next
  143.     m_SearchPath = New_SearchPath
  144.     PropertyChanged "SearchPath"
  145. End Property
  146. Public Property Get File() As String
  147. Attribute File.VB_Description = "The name of the file to search for."
  148. Attribute File.VB_HelpID = 2004
  149.     On Error Resume Next
  150.     File = m_File
  151. End Property
  152. Public Property Let File(ByVal New_File As String)
  153.     On Error Resume Next
  154.     m_File = New_File
  155.     PropertyChanged "File"
  156. End Property
  157. Private Property Get FilePart() As String
  158.     On Error Resume Next
  159.     If InStr(File, ".") > 0 Then
  160.         FilePart = Left(File, InStr(File, ".") - 1)
  161.     End If
  162. End Property
  163. Private Property Get ExtPart() As String
  164.     On Error Resume Next
  165.     ExtPart = Right(File, Len(File) - Len(FilePart))
  166. End Property
  167. Public Function Search() As String
  168. Attribute Search.VB_Description = "Used to find the location of a file specified in the File Property.  "
  169. Attribute Search.VB_HelpID = 3002
  170.     On Error Resume Next
  171.     Dim tmpBuf As String, l As Long
  172.     If Trim(File) = "" Then
  173.         Search = ""
  174.         Exit Function
  175.     End If
  176.     tmpBuf = Space(1025)
  177.     BinSearchPath SearchPath, FilePart, ExtPart, 1024, tmpBuf, l
  178.     tmpBuf = Left(tmpBuf, InStr(tmpBuf, vbNullChar))
  179.     Search = tmpBuf
  180. End Function
  181. Public Property Get PathExists(ByVal SeachPath As String) As Boolean
  182. Attribute PathExists.VB_Description = "Used to determine if a path exists on the users system."
  183. Attribute PathExists.VB_HelpID = 2017
  184.     On Error Resume Next
  185.     Dim l As Long
  186.     If Trim(SearchPath) = "" Then
  187.         PathExists = False
  188.         Exit Property
  189.     End If
  190.     l = GetAttr(Trim(SearchPath))
  191.     PathExists = (l = 16 And Err = 0)
  192. End Property
  193. Public Property Get Exists(ByVal SearchFile As String) As Boolean
  194. Attribute Exists.VB_Description = "A read only property to determine if a file exists on the users system."
  195. Attribute Exists.VB_HelpID = 2002
  196.     On Error Resume Next
  197.     Dim l As Long
  198.     If Trim(SearchFile) = "" Then
  199.         Exists = False
  200.         Exit Property
  201.     End If
  202.     l = GetAttr(Trim(SearchFile))
  203.     Exists = (l <> 16 And Err = 0)
  204. End Property
  205. Private Property Get isNT() As Boolean
  206.     On Error Resume Next
  207.     Dim OSystem As OSVERSIONINFO
  208.     OSystem.dwOSVersionInfoSize = 148
  209.     apiGetVersion OSystem
  210.     isNT = (OSystem.dwPlatformId = 2)
  211. End Property
  212. Public Property Get IsBinary(ByVal CheckFile As String) As Boolean
  213. Attribute IsBinary.VB_Description = "A read only property to determine if a file is Binary. (NT only)"
  214. Attribute IsBinary.VB_HelpID = 2007
  215.     On Error Resume Next
  216.     If Not isNT Then
  217.         On Error GoTo 0
  218.         Err.Raise 40004, Ambient.DisplayName, "IsBinary Property is only available under Windows NT"
  219.         IsBinary = False
  220.         Exit Property
  221.     End If
  222.     IsBinary = Not (BinaryType(CheckFile) = NonExecutable)
  223. End Property
  224. Public Property Get BinaryType(ByVal CheckFile As String) As CSBinaryTypes
  225. Attribute BinaryType.VB_Description = "A read only property to determine if a file is an executable and the type of executable."
  226. Attribute BinaryType.VB_HelpID = 2001
  227.     On Error Resume Next
  228.     If Not isNT Then
  229.         On Error GoTo 0
  230.         Err.Raise 40004, Ambient.DisplayName, "BinaryType Property is Only available under Windows NT"
  231.         BinaryType = NonExecutable
  232.         Exit Property
  233.     End If
  234.     If Not Exists(CheckFile) Then
  235.         BinaryType = NonExecutable
  236.         Exit Property
  237.     End If
  238.     Dim l As Long
  239.     If GetBinaryType(CheckFile, l) = 0 Then
  240.         BinaryType = NonExecutable
  241.         Exit Property
  242.     End If
  243.     Select Case l
  244.     Case SCS_32BIT_BINARY
  245.         BinaryType = BinaryWin_32Bit
  246.     Case SCS_DOS_BINARY
  247.         BinaryType = BinaryDos
  248.     Case SCS_WOW_BINARY
  249.         BinaryType = BinaryWin_16Bit
  250.     Case SCS_PIF_BINARY
  251.         BinaryType = BinaryPIF
  252.     Case SCS_POSIX_BINARY
  253.         BinaryType = BinaryPosix
  254.     Case SCS_OS216_BINARY
  255.         BinaryType = BinaryOS2_16Bit
  256.     Case Else
  257.         BinaryType = NonExecutable
  258.     End Select
  259. End Property
  260. Public Function SelectFile(Optional ByVal winTitle As String = "Select File", Optional ByVal initPath As String = "") As String
  261. Attribute SelectFile.VB_Description = "Used to call the standard Windows ""Browse for File"" Dialog box"
  262. Attribute SelectFile.VB_HelpID = 3006
  263.     On Error Resume Next
  264.     Dim OpenFile As OPENFILENAME
  265.     Dim lReturn As Long, pos As Long
  266.     OpenFile.lStructSize = Len(OpenFile)
  267.     OpenFile.hwndOwner = UserControl.Parent.hwnd
  268.     OpenFile.hInstance = App.hInstance
  269.     OpenFile.lpstrFilter = FileFilters
  270.     OpenFile.nFilterIndex = m_FilterIndex
  271.     OpenFile.lpstrFile = String(257, 0)
  272.     OpenFile.nMaxFile = 256
  273.     OpenFile.lpstrFileTitle = OpenFile.lpstrFile
  274.     OpenFile.nMaxFileTitle = OpenFile.nMaxFile
  275.     OpenFile.lpstrInitialDir = IIf(initPath = "", CurDir, initPath)
  276.     OpenFile.lpstrTitle = winTitle
  277.     OpenFile.Flags = &H20180C
  278.     lReturn = GetOpenFileName(OpenFile)
  279.     If lReturn <> 0 Then
  280.        pos = InStr(OpenFile.lpstrFile, Chr$(0))
  281.        If pos > 0 Then _
  282.            OpenFile.lpstrFile = Left(OpenFile.lpstrFile, pos - 1)
  283.        SelectFile = Trim(OpenFile.lpstrFile)
  284.     Else
  285.        SelectFile = ""
  286.     End If
  287. End Function
  288. Public Function SelectSave(Optional ByVal winTitle As String = "Save File", Optional ByVal initPath As String = "") As String
  289. Attribute SelectSave.VB_Description = "Used to call the standard Windows Save File Dialog"
  290. Attribute SelectSave.VB_HelpID = 3008
  291.     On Error Resume Next
  292.     Dim OpenFile As OPENFILENAME
  293.     Dim lReturn As Long, pos As Long
  294.     Dim sFilter As String
  295.     OpenFile.lStructSize = Len(OpenFile)
  296.     OpenFile.hwndOwner = UserControl.Parent.hwnd
  297.     OpenFile.hInstance = App.hInstance
  298.     OpenFile.lpstrFilter = FileFilters
  299.     OpenFile.nFilterIndex = m_FilterIndex
  300.     OpenFile.lpstrFile = String(257, 0)
  301.     OpenFile.nMaxFile = 256
  302.     OpenFile.lpstrFileTitle = OpenFile.lpstrFile
  303.     OpenFile.nMaxFileTitle = OpenFile.nMaxFile
  304.     OpenFile.lpstrInitialDir = IIf(initPath = "", CurDir, initPath)
  305.     OpenFile.lpstrTitle = winTitle
  306.     OpenFile.Flags = &H80000 + &H4 + &H200000 + &H8 + &H2 ' &H2000
  307.     lReturn = GetOpenFileName(OpenFile)
  308.     If lReturn <> 0 Then
  309.        pos = InStr(OpenFile.lpstrFile, Chr$(0))
  310.        If pos > 0 Then _
  311.            OpenFile.lpstrFile = Left(OpenFile.lpstrFile, pos - 1)
  312.        SelectSave = Trim(OpenFile.lpstrFile)
  313.     Else
  314.        SelectSave = ""
  315.     End If
  316. End Function
  317. Public Function SelectPath(Optional ByVal winTitle As String = "Select Path") As String
  318. Attribute SelectPath.VB_Description = "Used to call the ""Browse for Folder"" dialog to  allow the user to select a path."
  319. Attribute SelectPath.VB_HelpID = 3009
  320.     Dim bi As BROWSEINFO
  321.     Dim IDL As ITEMIDLIST
  322.     Dim pidl As Long
  323.     Dim r As Long
  324.     Dim pos As Integer
  325.     Dim spath As String
  326.     bi.hOwner = UserControl.hwnd
  327.     bi.pidlRoot = 0&
  328.     bi.lpszTitle = winTitle
  329.     bi.ulFlags = BIF_RETURNONLYFSDIRS
  330.     pidl = SHBrowseForFolder(bi)
  331.     If pidl > 0 Then
  332.         spath$ = Space$(1024)
  333.         r = SHGetPathFromIDList(ByVal pidl&, ByVal spath$)
  334.         If r Then
  335.                  pos = InStr(spath$, Chr$(0))
  336.                  SelectPath = Trim(Left(spath$, pos - 1))
  337.         Else: SelectPath = ""
  338.         End If
  339.     Else: SelectPath = ""
  340.     End If
  341. End Function
  342. 'Initialize Properties for User Control
  343. Private Sub UserControl_InitProperties()
  344.     On Error Resume Next
  345.     m_SearchPath = m_def_SearchPath
  346.     m_File = m_def_File
  347.     m_FilterIndex = m_def_FilterIndex
  348.     m_Filters = m_def_Filters
  349. End Sub
  350. 'Load property values from storage
  351. Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
  352.     On Error Resume Next
  353.     m_SearchPath = PropBag.ReadProperty("SearchPath", m_def_SearchPath)
  354.     m_File = PropBag.ReadProperty("File", m_def_File)
  355.     m_FilterIndex = PropBag.ReadProperty("FilterIndex", m_def_FilterIndex)
  356.     m_Filters = PropBag.ReadProperty("Filters", m_def_Filters)
  357. End Sub
  358. 'Write property values to storage
  359. Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
  360.     On Error Resume Next
  361.     Call PropBag.WriteProperty("SearchPath", m_SearchPath, m_def_SearchPath)
  362.     Call PropBag.WriteProperty("File", m_File, m_def_File)
  363.     Call PropBag.WriteProperty("FilterIndex", m_FilterIndex, m_def_FilterIndex)
  364.     Call PropBag.WriteProperty("Filters", m_Filters, m_def_Filters)
  365. End Sub
  366. Private Sub UserControl_Resize()
  367.     On Error Resume Next
  368.     Static amDoing As Boolean
  369.     If amDoing Then Exit Sub
  370.     amDoing = True
  371.     UserControl.Size 360, 330
  372.     amDoing = False
  373. End Sub
  374. Public Sub ShowAbout()
  375. Attribute ShowAbout.VB_UserMemId = -552
  376.     On Error Resume Next
  377.     dlgAbout.Show vbModal
  378.     Unload dlgAbout
  379. End Sub
  380.